package com.ruoyi.system.api.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.api.ResponseWithData;
import com.ruoyi.system.api.domain.ServiceInstrument;
import com.ruoyi.system.api.domain.User;
import com.ruoyi.system.api.mapper.ServiceInstrumentMapper;
import com.ruoyi.system.api.mapper.UserMapper;
import com.ruoyi.system.api.service.CcService;
import com.ruoyi.system.api.service.IUserService;

/**
 * 用户信息 (数据来源于CC端主动调用)Service业务层处理
 * 
 * @author shihx
 * @date 2020-02-25
 */
@Service
public class UserServiceImpl implements IUserService 
{
	
	 /**
	   * 配置文件
	   */
	@Autowired
	private Global global;
	
    @Autowired
    private UserMapper userMapper;

    /** 调用CC接口service */
    @Autowired
    private CcService ccService;

    /** 服务关联设备信息 */
    @Autowired
    private ServiceInstrumentMapper serviceInstrumentMapper;


    /**
     * 查询用户信息 (数据来源于CC端主动调用)
     * 
     * @param id 用户信息 (数据来源于CC端主动调用)ID
     * @return 用户信息 (数据来源于CC端主动调用)
     */
    @Override
    public User selectUserById(Long id)
    {
        return userMapper.selectUserById(id);
    }

    /**
     * 根据EnginnerId查询用户信息
     * @param enginnerId
     * @return
     */
    @Override
    public User selectUserByEnginnerId(String enginnerId){
        if (StringUtils.isNotEmpty(enginnerId)) {
            User user1 = new User();
            user1.setEnginnerId(Long.parseLong(enginnerId));
            List<User> users1 = userMapper.selectUserList(user1);
            if (users1 != null && users1.size() > 0) {
                return users1.get(0);
            }
        }
        return null;
    }

    /**
     * 查询用户信息 (数据来源于CC端主动调用)列表
     * 
     * @param user 用户信息 (数据来源于CC端主动调用)
     * @return 用户信息 (数据来源于CC端主动调用)
     */
    @Override
    public List<User> selectUserList(User user)
    {
        return userMapper.selectUserList(user);
    }

    /**
     * 新增用户信息 (数据来源于CC端主动调用)
     * 
     * @param user 用户信息 (数据来源于CC端主动调用)
     * @return 结果
     */
    @Override
    public int insertUser(User user)
    {
        user.setCreateTime(DateUtils.getNowDate());
        return userMapper.insertUser(user);
    }

    /**
     * 修改用户信息 (数据来源于CC端主动调用)
     * 
     * @param user 用户信息 (数据来源于CC端主动调用)
     * @return 结果
     */
    @Override
    public int updateUser(User user)
    {
        user.setUpdateTime(DateUtils.getNowDate());
        return userMapper.updateUser(user);
    }

    /**
     * 删除用户信息 (数据来源于CC端主动调用)对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteUserByIds(String ids)
    {
        return userMapper.deleteUserByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户信息 (数据来源于CC端主动调用)信息
     * 
     * @param id 用户信息 (数据来源于CC端主动调用)ID
     * @return 结果
     */
    @Override
    public int deleteUserById(Long id)
    {
        return userMapper.deleteUserById(id);
    }

    @Override
    public ResponseWithData<?> getFseInformation(String ServiceNoticeId, String EnginnerId) throws Exception {
        /** 调用CC端2.6获取TL名下工程师接口更新用户实时数据 (edit by wlf 2020-05-11 新增方法不更新工作状态!! 以修复文档Bug_ID: FSE_Bug_038)*/
        ccService.getEngineerUnupdateWorkday( ServiceNoticeId, EnginnerId, "getFseInformation");
        //ccService.getEngineerByTL( ServiceNoticeId, EnginnerId, "getFseInformation");
        /** 查询本地用户基本信息 */
        List<User> uList = userMapper.getFseInformation(EnginnerId);
        /** 根据服务单id查询设备号 */
        ServiceInstrument serviceInstrument = new ServiceInstrument();
        serviceInstrument.setServiceNoticeId(Long.parseLong(ServiceNoticeId));
        List<ServiceInstrument> slist = serviceInstrumentMapper.selectServiceInstrumentList(serviceInstrument);
        String level = "无";
        if(slist!=null && slist.size()>0){
            /** 调用CC端2.7获取FSE型号技能接口查询技能 */
            JSONObject jsonObject = ccService.getFseSkill( ServiceNoticeId, EnginnerId, "getFseInformation", slist.get(0).getInstrumentNo());
            if(jsonObject.getInteger("code")==200){
                level = jsonObject.getJSONArray("record").getJSONObject(0).getString("Level");
            }
        }
       
        if(!uList.isEmpty()){
           uList.get(0).setLevel(level);
           String beginTime=global.getWorkBeginTime();
       	   String endTime=global.getWorkEndTime();
           boolean isTimeSlot=DateUtils.isTimeSlot(beginTime,endTime);
       	   boolean isDatePeriod=DateUtils.isDatePeriod();
       	   if(!isTimeSlot||!isDatePeriod){
       		uList.get(0).setAddress("");
       	   }
        }
       

        return new ResponseWithData<>().success(uList).msg("查询成功").code(200);
    }

    @Override
    public ResponseWithData<?> getStUserMessage(String enginnerId) throws Exception {
        /** 查询本地用户基本信息 */
        List<User> uList = userMapper.getFseInformation(enginnerId);
        if(!uList.isEmpty()){
            String beginTime=global.getWorkBeginTime();
        	String endTime=global.getWorkEndTime();
            boolean isTimeSlot=DateUtils.isTimeSlot(beginTime,endTime);
            boolean isDatePeriod=DateUtils.isDatePeriod();
            
        	if(!isTimeSlot||!isDatePeriod){
        		uList.get(0).setAddress("");
        	   }
         }
        return new ResponseWithData<>().success(uList).msg("查询成功").code(200);
    }

    @Override
    public ResponseWithData<?> updateRegistrationId(String registrationId, String EnginnerId) throws Exception {
        userMapper.updateUserRegistrationId(registrationId);
        User user = new User();
        user.setRegistrationId(registrationId);
        user.setEnginnerId(Long.parseLong(EnginnerId));
        userMapper.updateRegistrationId(user);
        return new ResponseWithData<>().success().msg("更新成功").code(200);
    }

    @Override
    public int updateUserByEnginnerId(User user) {
        return userMapper.updateUserByEnginnerId(user);
    }

    @Override
    public ResponseWithData<?> checkUserWorkDay(String enginnerId) throws Exception {
//        String state = "休假";
        User user = new User();
        user.setEnginnerId(Long.parseLong(enginnerId));
        List<User> users = userMapper.selectUserList(user);
//        if(users!=null && users.size()>0 ){
//            String workDay = "0";
//            if(workDay.equals(users.get(0).getWorkday())){
//                state = "上班";
//            }
//        }
        return new ResponseWithData<>().success(users).msg("查询成功").code(200);
    }
}
